package mycommunity.web.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import mycommunity.biz.ForumBiz;
import mycommunity.biz.TopicBiz;
import mycommunity.biz.UserBiz;
import mycommunity.entity.Forum;
import mycommunity.entity.Role;
import mycommunity.entity.User;

@Controller
public class IndexController {

	@Autowired
	private ForumBiz forumBiz;
	
	@Autowired
	private UserBiz userBiz;
	
	@Autowired
	private TopicBiz topicBiz;
	
	@RequestMapping("/index")
	public String index(Model model) {
		model.addAttribute("forums",forumBiz.getAll());
		model.addAttribute("topic",topicBiz.geAllTopic(0, null, 1, 6));
		return "index";
	}
	
	@RequestMapping(value="/register",method=RequestMethod.GET)
	public String register() {
		return "register";
	}
	
	@RequestMapping(value="/checkUsername",method=RequestMethod.GET)
	@ResponseBody
	public boolean checkUsername(String username) {
		if (userBiz.checkUserName(username)==null) {
			return true;
		}else {
			return false;
		}
	}
	
	@RequestMapping(value="/addUser",method=RequestMethod.POST)
	public String addUser(User user,HttpSession session) {
		user.setRole(new Role(1, null));
		userBiz.addUser(user);
		session.setAttribute("loginUser", user);
		return "redirect:/index";
	}
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login() {
		return "login";
	}
	
	@RequestMapping(value="/checkUser",method=RequestMethod.POST)
	public String checkUser(String username,String password,Model model,HttpSession session) {
		if (userBiz.checkUser(username, password)==null) {
			model.addAttribute("error","用户名或密码错误");
			return "login";
		}else {
			session.setAttribute("loginUser", userBiz.checkUser(username, password));
			return "redirect:/index";
		}
	}
	
	@RequestMapping(value="/loginOut",method=RequestMethod.GET)
	public String loginOut(HttpSession session) {
		session.removeAttribute("loginUser");
		return "redirect:/login";
	}
	
	@RequestMapping(value="/topic",method=RequestMethod.GET)
	public String topic(Model model, int fid,
			@RequestParam(name="keyWord",required=false,defaultValue="")String keyWord,
			@RequestParam(name="pageNum",required=false,defaultValue="1")int pageNum) {
		final int pageSize = 6;
		model.addAttribute("forum", forumBiz.getForumByid(fid));
		model.addAttribute("bestTopic", topicBiz.getTopics(fid, keyWord, 1, pageSize, true));
		model.addAttribute("normalTopic", topicBiz.getTopics(fid, keyWord, pageNum, pageSize, false));
		int row = topicBiz.getRow(fid, keyWord);
		int totalPage = row%pageSize==0?row/pageSize:row/pageSize+1;
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("pageNum", pageNum);
		return "topic";
	}
}
